//
//  HotViewcontroller.m
//  YSBEmployee
//
//  Created by developer on 2021/11/30.
//  Copyright © 2021 ysb. All rights reserved.
//

#import "YSBChildOrderDetailViewController.h"

#import "YSBOrderDetailNavTableViewCell.h"

#import "YSBAccountStatusViewController.h"

#import "YSBSpecialRequirementsView.h"
#import "YSBAssessmentOrderSingleView.h"

#import "YSBOrderDetailNavTableViewCell.h"

#import "YSBChildOrderDetailOrderInfoTableViewCell.h"


@interface YSBChildOrderDetailViewController : YSBViewController

@property (strong, nonatomic) UIView *maskBottomView;

@end



@implementation YSBChildOrderDetailViewController

- (void)configurationUIData {
    
    //从可接订单或收藏订单列表进入详情需要隐藏
    if (self.newOrderInfoType == 14 ||
        self.newOrderInfoType == 13) {
        [self bottomView];
        [self.bottomView addSubview:self.maskBottomView];
        [self.maskBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.mas_equalTo(self.bottomView);
            make.right.mas_equalTo(self.bottomView).offset(-(95));
        }];
    }
    
}


- (UIView *)maskBottomView{
    if (!_maskBottomView) {
        _maskBottomView = [[UIView alloc] init];
        _maskBottomView.backgroundColor = [UIColor whiteColor];
    }
    return _maskBottomView;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 2) {
        
        if (indexPath.row == 0) {
            if (self.model.appealModel.appeal_id.integerValue > 0) {
                YSBChildOrderDetailExceptionsComplaintViewController *vc = [[YSBChildOrderDetailExceptionsComplaintViewController alloc] init];
                vc.task_id = self.model.ID;
                vc.order_id = self.oid;
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                if (self.model.status == -1 && ![self.model.cancel_type isEqualToString:@"系统取消"]) {//待确认时或非系统取消的取消订单,禁止查看地图
                    return;
                }
                else if (self.model.status == 6 || self.model.status == 7) {//待同意或待发放
                    return;
                }
                else{
                    //查看地图
                    if([[tableView cellForRowAtIndexPath:indexPath] isKindOfClass:[YSBOrderDetailNavTableViewCell class]]){
                        YSBLookAtMapViewController *vc = [[YSBLookAtMapViewController alloc] init];
                        YSBOrderModel *model = [[YSBOrderModel alloc] init];
                        model.company_name = self.model.company_name.length > 0 ? self.model.company_name : @"";
                        model.address = self.model.address.length > 0 ? self.model.address : @"";
                        if (![YSBCommonTool isBlankString:self.model.work_address_lat]) {
                            model.work_address_lat = self.model.work_address_lat;
                        }
                        else{
                            if (![YSBCommonTool isBlankString:self.model.lat]) {
                                model.work_address_lat = self.model.lat;
                            }
                        }
                        if (![YSBCommonTool isBlankString:self.model.work_address_lon]) {
                            model.work_address_lon = self.model.work_address_lon;
                        }
                        else{
                            if (![YSBCommonTool isBlankString:self.model.lon]) {
                                model.work_address_lon = self.model.lon;
                            }
                        }
                        vc.model = model;
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                }
            }
            
        }else{
            if (self.model.status == -1 && ![self.model.cancel_type isEqualToString:@"系统取消"]) {//非系统取消的取消订单,禁止查看地图
                return;
            }else{
                //查看地图
                if([[tableView cellForRowAtIndexPath:indexPath] isKindOfClass:[YSBOrderDetailNavTableViewCell class]]){
                    YSBLookAtMapViewController *vc = [[YSBLookAtMapViewController alloc] init];
                    YSBOrderModel *model = [[YSBOrderModel alloc] init];
                    model.company_name = self.model.company_name.length > 0 ? self.model.company_name : @"";
                    model.address = self.model.address.length > 0 ? self.model.address : @"";
                    model.work_address_lat = self.model.work_address_lat;
                    model.work_address_lon = self.model.work_address_lon;
                    vc.model = model;
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }
        }
        
    }
    
}


@end




@implementation YSBOrderDetailNavTableViewCell

// 根据是否接单来处理未接单地图的显示
- (void)showShowViewActionWithIsReceived:(BOOL)isReceived {
    
    if (isReceived) {
        if (self.model.status == -1 && ![self.model.cancel_type isEqualToString:@"系统取消"]) {//非系统取消的取消订单,隐藏查看地图
            self.rightIconImgView.hidden = YES;
            self.lookAtLabel.hidden = YES;
            self.addressCopyLabel.hidden = YES;
        }else{
            self.rightIconImgView.hidden = NO;
            self.lookAtLabel.hidden = NO;
            self.addressCopyLabel.hidden = NO;
        }
    }
    else{ //未接单的情况
        self.rightIconImgView.hidden = NO;
        self.lookAtLabel.hidden = NO;
        self.addressCopyLabel.hidden = NO;
    }
    
}

@end



@implementation YSBAccountStatusViewController


#pragma mark - 抢单
- (void) takeTaskOrder:(YSBOrderModel *)order {
     
    //未接单的订单oid等于id.由于之前用oid这个值,接口重构后用id代替订单id.
    order.oid = order.ID;

    if (order.ID == 0) {
        return;
    }
#pragma mark - 特殊要求提醒
    NSDictionary *parameter = @{
        @"order_id" : order.ID
    };
    
    [InterfaceSwitchingLogic requestJobOrderDetailWithParameter:parameter andComplete:^(NSNumber *code, NSDictionary *dict) {
        
        YSBJobOrderDetailModel *detailModel = [YSBJobOrderDetailModel yy_modelWithJSON:dict];
        if (detailModel.other_contents) {
            if (![YSBCommonTool isBlankString:detailModel.other_contents]) { //是否有特殊要求进行弹框提示
                
                NKAlertView *alertView = [[NKAlertView alloc] init];
                CGFloat boxWidth  = [[UIScreen mainScreen] bounds].size.width - 60;
                CGFloat boxHeight = [[UIScreen mainScreen] bounds].size.height * 0.6;
                
                YSBSpecialRequirementsView *requirementsView = [[YSBSpecialRequirementsView alloc] initWithFrame:CGRectMake(0, 0, boxWidth, boxHeight) title:@"特殊要求" andJobOrderDetailModel:detailModel leftButtonTitle:@"再考虑一下" rightButtonTitle:@"确认抢单" andLeftAction:^{
                } andRightAction:^{//右边按钮事件
                    // 是否连单提示
                    [self continuousOrderTipsWithOrder:order];
                }];
                alertView.contentView = requirementsView;
                [alertView show];
                
            }
            else{
                // 是否连单提示
                [self continuousOrderTipsWithOrder:order];
            }
        }
        
    }];
    
    
}


#pragma mark - 连单提示
- (void)continuousOrderTipsWithOrder:(YSBOrderModel *)order {

    order.oid = order.ID;
    order.work_days = order.days;
    NSInteger days = order.days.integerValue;
    
    if (order.days.integerValue > 1) {
        // 连单弹窗
        NKAlertView *alertView = [[NKAlertView alloc] init];
        SnatchingOrdersView *moreOrderTipsView = [[SnatchingOrdersView alloc] initWithFrame:CGRectMake(0, 0, 280, 230) title:@"抢单提示" andDays:days andTopTips:order.grab_orders_tip leftButtonTitle:@"不去了" rightButtonTitle:@"去" andLeftAction:^{
        } andRightAction:^{//右边按钮事件
            [self _gotoWebViewWithOrder:order];
        }];
        alertView.contentView = moreOrderTipsView;
        [alertView show];
        
    }
    else {
        [self _gotoWebViewWithOrder:order];
    }
}



- (void) gotoOrderDetailForOrder:(YSBOrderModel *)order hasMatched:(BOOL)hasMatched {
    
    if (hasMatched) {//已被抢
        YSBChildOrderDetailViewController *vc = [[YSBChildOrderDetailViewController alloc] init];
        vc.detailid = self.abnormalInfoModel.assess_info.tid;
        vc.newOrderInfoType = 10;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else {//未接单
        YSBChildWaitingOrderDetailViewController *vc = [[YSBChildWaitingOrderDetailViewController alloc] init];
        order.oid = order.ID;
        vc.detailid = order.oid;
        vc.newOrderInfoType = 19;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    
}

@end



@implementation YSBAssessmentOrderSingleView

- (void) setModel:(YSBOrderModel *)model isDetail: (BOOL)isDetail index: (int)index {
    self.isDetail = isDetail;
    self.model = model;
    self.index = index;
    
    self.sepLine.hidden = index <= 0;
    
    self.titleLabel.text = ![YSBCommonTool isBlankString:model.title] ? model.title : @"-";
    
    NSString *priceStr = ![YSBCommonTool isBlankString:model.price] ? model.price : @"-";
    if ([priceStr containsString:@"."]) {
        priceStr = priceStr.trimmedBy(@"0");
        priceStr = priceStr.trimmedBy(@".");
    }
    priceStr = [NSString stringWithFormat:@"%@元/小时", priceStr];
    self.priceLabel.text = priceStr;
    
    //标签处理
    NSMutableArray *labelArray = [[NSMutableArray alloc] init];
    if (![YSBCommonTool isBlankString:model.duration_text]) {//上班时长
        [labelArray addObject:model.duration_text];
    }
    if (![YSBCommonTool isBlankString:model.insurance_text]) {//保险
        [labelArray addObject:model.insurance_text];
    }
    if (![YSBCommonTool isBlankString:model.is_do_text]) {//是否做过
        [labelArray addObject:model.is_do_text];
    }
    //当订单需求人数-匹配人数>=2时，才显示“可分享订单”，否则不显示
    if (![YSBCommonTool isBlankString:model.share_text] ||
        model.share_status == 1 ||
        (model.number.integerValue - model.match_number.integerValue >= 2)) {//是否可分享
        [labelArray addObject:@"可分享订单"];
    }
    
    NSArray *list = [labelArray copy];
    
    
    //连续发单天数
    NSInteger continueDays = model.days.integerValue;
    //是否白班还是夜班
    BOOL isDay = NO;
    if (![YSBCommonTool isBlankString:model.work_text]) {
        isDay = [model.work_text isEqualToString:@"白班"];
    }
    
    [self.listView setTitles:list isDay:isDay continueDays:continueDays ];
    
    //上班日期
    NSString *timeStr = @"";
    if (![YSBCommonTool isBlankString:model.work_date]) {
        timeStr = model.work_date;
    }
    if (![YSBCommonTool isBlankString:model.work_time]) {
        timeStr = [NSString stringWithFormat:@"%@ %@", timeStr, model.work_time];
    }
    if (![YSBCommonTool isBlankString:model.work_date_text]) {
        timeStr = [NSString stringWithFormat:@"%@(%@)", timeStr, model.work_date_text];
    }
    
    self.timeLabel.text = [NSString stringWithFormat:@"上班日期: %@", timeStr];
    
    //距离
    NSString *distanceStr = @"";
    if (![YSBCommonTool isBlankString:model.township]) {
        distanceStr = model.township;
    }
    if (![YSBCommonTool isBlankString:model.distance_text]) {
        if (distanceStr.length) {
            distanceStr = [distanceStr stringByAppendingString:@"·"];
        }
        distanceStr = [NSString stringWithFormat:@"%@%@", distanceStr, model.distance_text];
    }
    
    self.distanceLabel.text = distanceStr;
    
    self.titleLabel.alpha = 1;
    self.priceLabel.alpha = 1;
    
    if (self.isDetail) {
        // 查看详情
        self.actionButton.enabled = YES;
        
        [self.actionButton setBackgroundImage:[UIImage defaultV3] forState:(UIControlStateNormal)];
        [self.actionButton setTitle:@"查看详情" forState:(UIControlStateNormal)];
        
    }
    else {
        // 订单是否在匹配中 1：是 = 可以抢 0：否 被抢了
        if (model.matching.boolValue) {
            self.actionButton.enabled = YES;
            
            [self.actionButton setBackgroundImage:[UIImage defaultV3] forState:(UIControlStateNormal)];
            [self.actionButton setTitle:@"立即抢单" forState:(UIControlStateNormal)];
        }
        else {
            self.titleLabel.alpha = 0.5;
            self.priceLabel.alpha = 0.5;
            
            self.actionButton.enabled = NO;
            
            [self.actionButton setBackgroundImage:nil forState:(UIControlStateDisabled)];
            [self.actionButton setBackgroundImage:nil forState:(UIControlStateNormal)];
            self.actionButton.backgroundColor = UIColor.blackColor.nn_alpha(0.2);
            [self.actionButton setTitle:@"被抢了" forState:(UIControlStateDisabled)];
            
            self.tap.enabled = NO;
        }
    }
}

@end



@implementation YSBOrderDetailNavTableViewCell

#pragma mark - Action
- (void)touchTap:(UITapGestureRecognizer *)sender{
    
    NSString *address = self.model.address;
    if ([YSBCommonTool isBlankString:address]) {
        address = self.waitingModel.address;
    }
    
    if ([YSBCommonTool isBlankString:address]) {
        [SVProgressHUD showInfoWithStatus:@"复制失败"];
    }else{
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = [YSBCommonTool isBlankString:address] ? @"" : address;
        if (pasteboard != nil) {
            [SVProgressHUD showInfoWithStatus:@"复制成功"];
        }
    }
    
}


@end




@interface YSBChildOrderDetailOrderInfoTableViewCell ()

@property (strong, nonatomic) UIView *containView;//容器

@property (nonatomic, strong) YSBOrderListCellTitleView *orderView;
@property (nonatomic, strong) YSBOrderListCellTitleView *orderPublishTimeView;
@property (nonatomic, strong) YSBOrderListCellTitleView *orderEndTimeView;

@property (nonatomic, strong) YSBOrderListCellTitleView *fourthTimeView;
@property (nonatomic, strong) YSBOrderListCellTitleView *fifthTimeView;

@property (strong, nonatomic) UILabel *copyLabel;


@end

@implementation YSBChildOrderDetailOrderInfoTableViewCell



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
                
        [self addSubviews];
        [self prepare];
        
    }
    return self;
}


- (void)addSubviews {
    self.backgroundColor = [UIColor colorWithHex:0xf5f5f5 alpha:1];

    [self.contentView addSubview:self.containView];
    [self.containView addSubview:self.orderView];
    [self.containView addSubview:self.orderPublishTimeView];
    [self.containView addSubview:self.orderEndTimeView];
    [self.containView addSubview:self.fourthTimeView];
    [self.containView addSubview:self.fifthTimeView];
    

    [self.containView addSubview:self.copyLabel];

    
}


- (void)prepare {
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(10);
        make.right.mas_equalTo(self).offset(-10);
        make.top.bottom.mas_equalTo(self);
    }];
    
    //中间内容
    [self.orderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.containView).offset(20);
        make.left.mas_equalTo(self.containView).offset(10);
        make.right.mas_equalTo(self.containView).offset(-50);
    }];
    
    [self.orderPublishTimeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.orderView.mas_bottom).offset(15);
        make.left.right.mas_equalTo(self.orderView);
    }];
    
    [self.orderEndTimeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.orderPublishTimeView.mas_bottom).offset(15);
        make.left.right.mas_equalTo(self.orderView);
    }];
    
    [self.fourthTimeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.orderEndTimeView.mas_bottom).offset(15);
        make.left.right.mas_equalTo(self.orderView);
    }];
    
    [self.fifthTimeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.fourthTimeView.mas_bottom).offset(15);
        make.left.right.mas_equalTo(self.orderView);
    }];
    
    [self.copyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.orderView);
        make.left.mas_equalTo(self.orderView.mas_right).offset(10);
        make.right.mas_equalTo(self.containView).offset(-10);
        make.height.mas_equalTo(@(30));
    }];

    
}



#pragma mark - Action
- (void)touchTap:(UITapGestureRecognizer *)sender{
    
    NSString *orderNo = self.model.order_no;
    if ([YSBCommonTool isBlankString:orderNo]) {
        orderNo = self.waitingModel.order_no;
    }
    
    if ([YSBCommonTool isBlankString:orderNo]) {
        [SVProgressHUD showInfoWithStatus:@"复制失败"];
    }else{
        UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
        pasteboard.string = [YSBCommonTool isBlankString:orderNo] ? @"" : orderNo;
        if (pasteboard != nil) {
            [SVProgressHUD showInfoWithStatus:@"复制成功"];
        }
    }
    
}


#pragma mark - Lazy
- (UIView *)containView{
    if (!_containView) {
        _containView = [[UIView alloc] init];
        _containView.backgroundColor = [UIColor whiteColor];
        _containView.layer.cornerRadius = 8;
        _containView.clipsToBounds = YES;
    }
    return _containView;
}

- (YSBOrderListCellTitleView *)orderView{
    if (!_orderView) {
        _orderView = [[YSBOrderListCellTitleView alloc] init];
        _orderView.titleLab.textColor = [UIColor colorWithHex:0x999999 alpha:1];
    }
    return _orderView;
}

- (YSBOrderListCellTitleView *)orderPublishTimeView{
    if (!_orderPublishTimeView) {
        _orderPublishTimeView = [[YSBOrderListCellTitleView alloc] init];
        _orderPublishTimeView.titleLab.textColor = [UIColor colorWithHex:0x999999 alpha:1];
    }
    return _orderPublishTimeView;
}

- (YSBOrderListCellTitleView *)orderEndTimeView{
    if (!_orderEndTimeView) {
        _orderEndTimeView = [[YSBOrderListCellTitleView alloc] init];
        _orderEndTimeView.titleLab.textColor = [UIColor colorWithHex:0x999999 alpha:1];
    }
    return _orderEndTimeView;
}

- (YSBOrderListCellTitleView *)fourthTimeView{
    if (!_fourthTimeView) {
        _fourthTimeView = [[YSBOrderListCellTitleView alloc] init];
        _fourthTimeView.titleLab.textColor = [UIColor colorWithHex:0x999999 alpha:1];
        _fourthTimeView.hidden = YES;
    }
    return _fourthTimeView;
}

- (YSBOrderListCellTitleView *)fifthTimeView{
    if (!_fifthTimeView) {
        _fifthTimeView = [[YSBOrderListCellTitleView alloc] init];
        _fifthTimeView.titleLab.textColor = [UIColor colorWithHex:0x999999 alpha:1];
        _fifthTimeView.hidden = YES;
    }
    return _fifthTimeView;
}

- (UILabel *)copyLabel{
    if (!_copyLabel) {
        _copyLabel = [[UILabel alloc] init];
        _copyLabel.text = @"复制";
        _copyLabel.textColor = [UIColor colorWithHex:0x1e8dff alpha:1];
        _copyLabel.textAlignment = 2;
        _copyLabel.font = [UIFont systemFontOfSize:14];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchTap:)];
        [_copyLabel addGestureRecognizer:tap];
        _copyLabel.userInteractionEnabled = YES;
    }
    return _copyLabel;
}


@end
